package com.sw.bbs.user.controller;

import com.sw.bbs.common.common.GlobalException;
import com.sw.bbs.user.pojo.User;
import com.sw.bbs.user.service.UserService;
import com.sw.bbs.user.vo.ApplyRequest;
import com.sw.bbs.user.vo.LoginRequest;
import com.sw.bbs.user.vo.RegisterRequest;
import com.sw.bbs.user.vo.SendCodeRequest;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * @Desc: No Desc
 * @Author: wusonghe
 * @Date: 2019/3/5 17:18
 */
@Api(value = "用户授权模块", description = "用户授权", tags = {"User Module"})
@RestController("/user/")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;



    @PostMapping("/login")
    @ApiOperation(value = "登陆接口", notes = "登陆", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 200, message = "分页结果结构", response = ResponseEntity.class)})
    public ResponseEntity<?> login(@ApiParam @ModelAttribute LoginRequest loginRequest)
            throws GlobalException {
        try {
            Map result = userService.loginBy(loginRequest);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            logger.error("login", e);
            throw new GlobalException("/user/login", e.getMessage());
        }
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册接口", notes = "注册", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 200, message = "分页结果结构", response = ResponseEntity.class)})
    public ResponseEntity<?> register(@ApiParam @ModelAttribute RegisterRequest registerRequest)
            throws GlobalException {
        try {
            Map result =  userService.registerBy(registerRequest);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            logger.error("register", e);
            throw new GlobalException("/user/register", e.getMessage());
        }

    }

    @PostMapping("/sendCode")
    @ApiOperation(value = "发送验证码接口", notes = "发送验证码", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 200, message = "结果结构", response = ResponseEntity.class)})
    public ResponseEntity<?> sendCode(@ApiParam @ModelAttribute SendCodeRequest sendCodeRequest)
            throws GlobalException {
        try {
            Integer result = userService.sendCode(sendCodeRequest);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            logger.error("sendCode", e);
            throw new GlobalException("/user/sendCode", e.getMessage());
        }
    }

    @PostMapping("/detail")
    @ApiOperation(value = "详情接口", notes = "详情", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 200, message = "结果结构", response = User.class)})
    public ResponseEntity<?> detail(@RequestParam String token)
            throws GlobalException {
        try {
            User user = userService.getUserByToken(token);
             return new ResponseEntity<>(user, HttpStatus.OK);
        } catch (Exception e) {
            logger.error("list", e);
            throw new GlobalException("/user/list", e.getMessage());
        }

    }

    //todo :

    /**
     * # 用户
     *
     * - 申请成为版主，副版主
     * - 设置为管理员，版主，副版主
     * - 设置角色功能权限
     *      - 版主
     * 	        - 专栏
     * 	        - 管理板块
     */
    @PostMapping("/apply")
    @ApiOperation(value = "申请接口", notes = "申请", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 200, message = "结果结构", response = User.class)})
    public ResponseEntity<?> apply(@ApiParam @ModelAttribute ApplyRequest applyRequest)
            throws GlobalException {
        try {
            return new ResponseEntity<>(null, HttpStatus.OK);
        } catch (Exception e) {
            logger.error("apply", e);
            throw new GlobalException("/user/apply", e.getMessage());
        }

    }

    @PostMapping("/setRole")
    @ApiOperation(value = "设置接口", notes = "设置", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 200, message = "结果结构", response = User.class)})
    public ResponseEntity<?> setRole(@ApiParam @ModelAttribute ApplyRequest applyRequest)
            throws GlobalException {
        try {
            return new ResponseEntity<>(null, HttpStatus.OK);
        } catch (Exception e) {
            logger.error("apply", e);
            throw new GlobalException("/user/apply", e.getMessage());
        }

    }

}
